package common.db;

import common.db.repository.SqlQueryGenerator;
import common.db.repository.SqlQueryParam;
import common.db.handlers.ResultSetHandler;
import common.db.io.MyLog;

/**
 * Created by IntelliJ IDEA.
 * User: Paskal
 * Date: Apr 26, 2009
 * Time: 10:18:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class AdvancedQueryRunner extends QueryRunner
{
	private SqlQueryGenerator sqlGenerator;


	public AdvancedQueryRunner(ConnectionParametar dbParam, SqlQueryGenerator sqlGenerator)
	{
		super(dbParam);
		this.sqlGenerator = sqlGenerator;
	}

	public AdvancedQueryRunner(ConnectionParametar dbParam, String sqlGen)
	{
		super(dbParam);
		this.sqlGenerator = new SqlQueryGenerator(sqlGen);
	}


	public Object Insert(String mappingTable, Object entity, String... view)
	{
		SqlQueryParam sqlParam = sqlGenerator.getInsert(mappingTable, entity, view);
		return insertWithKeyReturn(sqlParam);
	}

	public int Update(String mappingTable, Object entity, String... view)
	{
		SqlQueryParam sqlParam = sqlGenerator.getUpdate(mappingTable, entity, view);
    	return update(sqlParam);
	}

	public int Delete(String mappingTable, Object entity, String... view)
	{
		SqlQueryParam sqlParam = sqlGenerator.getDelete(mappingTable, entity, view);
		return update(sqlParam);
	}

	public <E> E Select(String mappingTable, Object entity, ResultSetHandler<E> rsh, String... view)
	{
		SqlQueryParam sqlParam = sqlGenerator.getSelect(mappingTable, entity, view);
		return query(sqlParam, rsh);
	}

	public Object[] InsertWithMultyRet(String mappingTable, Object entity, String... view)
	{
		SqlQueryParam sqlParam = sqlGenerator.getInsert(mappingTable, entity, view);
		return insertWithMultyReturn(sqlParam);
	}

	public <E> E Query(String mappingTable, String view, ResultSetHandler<E> rsh, Object... params)
	{
		String sql = sqlGenerator.getSelectFrom(mappingTable, view);//.getMappingResourcer().query(sqlName);
		return query(sql, rsh, params);
	}

	public <E> E Query(String mappingTable, ResultSetHandler<E> rsh, Object... params)
	{
		String sql = sqlGenerator.getSelectFrom(mappingTable, "default");//.getMappingResourcer().query(sqlName);
		return query(sql, rsh, params);
	}

	public int DeleteByParams(String mappingTable, String view, Object... params)
	{
		String sql = sqlGenerator.getDeleteFrom(mappingTable, view);
		return update(sql, params);
	}

	public int DeleteDefaultByParams(String mappingTable, Object... params)
	{
		String sql = sqlGenerator.getDeleteFrom(mappingTable, "default");
		return update(sql, params);
	}
}
